<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="生产任务">
      {{ billNo }}
    </el-form-item>
    <el-form-item label="订单日期">
      <div class="block">
        <el-date-picker
            v-model="taskTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="领料时间">
      <div class="block">
        <el-date-picker
            v-model="pickTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="补料时间">
      <div class="block">
        <el-date-picker
            v-model="supplementTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="退料时间">
      <div class="block">
        <el-date-picker
            v-model="returnTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="变品时间">
      <div class="block">
        <el-date-picker
            v-model="desaltingTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="入库时间">
      <div class="block">
        <el-date-picker
            v-model="prdInTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item>
      <el-checkbox v-model="checked">只传调拨单</el-checkbox>
      <el-checkbox v-model="checkTask" disabled>同品不同仓库</el-checkbox>
    </el-form-item>
    <!--  无法传单的提示  -->
    <el-form-item>
    <span style="font-size: 18px; padding-bottom: 10px; color: red; font-weight: bold">
      无法传入金蝶的单据类型：
    </span>
    </el-form-item>
    <el-form-item>
    <span style="font-size: 18px; padding-bottom: 10px; color: red; font-weight: bold">
      生产任务中只有变品退料批次号包含BN开头的物料
    </span>
    </el-form-item>
    <el-form-item>
    <span style="font-size: 18px; padding-bottom: 10px; color: red; font-weight: bold">
      生产任务中只有领料入库批次号包含BN开头的物料（领料批次号不包含BN开头的物料）
    </span>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即上传</el-button>
      <el-button type="warning" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
  <!-- 遮罩层 -->
  <div v-if="uploadLoading" class="fullscreen-mask">
    <div style="position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5);
    z-index:999; display:flex; justify-content:center; align-items:center;"></div>
  </div>
</template>
<script>
import dayjs from "dayjs";
export default {
  data() {
    return {
      taskTime: '',
      pickTime: '',
      supplementTime: '',
      returnTime: '',
      desaltingTime: '',
      prdInTime: '',
      billNo: '',
      loading: false,
      type: '',
      createTime: '',
      checked: false,
      checkTask: false, //同品通批次入到不同的仓库
      uploadLoading: false //全屏遮罩默认关闭
    }
  },
  async mounted() {
    this.billNo = this.$route.query.billNo;
    this.type = this.$route.query.type;
    this.createTime = this.$route.query.createTime;
    this.checkPrdTask();
  },
  methods: {
    formatDate(date) {
      if (!date) return ''
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    async uploadKingdee() {
      this.loading = true
      try {
        let res = await this.callApi('upload_kingdee_task_bn', {
          taskTime: dayjs(this.taskTime).format('YYYY-MM-DD HH:mm:ss'),
          pickTime: dayjs(this.pickTime).format('YYYY-MM-DD HH:mm:ss'),
          supplementTime: dayjs(this.supplementTime).format('YYYY-MM-DD HH:mm:ss'),
          returnTime: dayjs(this.returnTime).format('YYYY-MM-DD HH:mm:ss'),
          desaltingTime: dayjs(this.desaltingTime).format('YYYY-MM-DD HH:mm:ss'),
          prdInTime: dayjs(this.prdInTime).format('YYYY-MM-DD HH:mm:ss'),
          billNo: this.billNo,
          type: this.type,
          createTime: this.createTime,
          checked: this.checked
        })
        if (!res.res) {
          return
        }
        this.uploadLoading = false; // 上传完成关闭遮罩
        this.$message.success(`上传成功`)
        setTimeout(() => {
          window.opener.location.reload()
          window.close()
        }, 1000)
      } finally {
        this.loading = false
      }
    },
    async onSubmit() {
      //校验车间收货是否都入库
      this.loading = true
      let checkRes = true;
      try {
        let res = await this.callApi('check_prd_col_in_stock', {billNo: this.billNo})
        checkRes = res.data;
      } finally {
        this.loading = false
      }
      if (!checkRes) {
        this.$message.warning('存在没有入库的车间收货单')
        return
      }
      this.$confirm('是否确认上传金蝶', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        //拦截弹窗关闭行为
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            done(); //关闭确认弹窗
            this.uploadLoading = true;//开启全屏遮罩
            //显示正在上传提示
            const loadingMsg = this.$message({
              message: '正在上传',
              iconClass: 'el-icon-loading',
              duration: 0, //永久显示直到关闭
              showClose: false
            });
            //执行上传
            this.uploadKingdee()
                .finally(() => {
                  loadingMsg.close(); //关闭上传提示
                  instance.confirmButtonLoading = false;
                });
          } else {
            done(); // 取消操作直接关闭弹窗
          }
        }
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '取消上传'
        })
      })
    },
    //校验有没有同品通批次入到不通仓库的
    async checkPrdTask() {
      this.loading = true
      try {
        let res = await this.callApi('check_prd_task', {billNo: this.billNo})
        console.log(res.data)
        this.checkTask = res.data
      } finally {
        this.loading = false
      }
    },
    //取消并关闭页面
    async cancel() {
      this.$confirm('确定关闭并返回', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false
      }).then(() => {
        window.close()
      });
    }
  }
}
</script>

<style scoped lang="scss">

</style>
